﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace SuperKit
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application {
        public static ArgReader Args => ArgReader.InitialArgs;

        public const string STARTUP_DEFAULT = "Default";
        public const string STARTUP_RENAME = "Rename";
        public const string STARTUP_GALLERY = "Gallery";
        public const string STARTUP_VIDEO = "Video";
        public const string STARTUP_HTML = "Html";

        private static Image? _checkImage;
        public static Image GetCheckImage() {
            if (_checkImage == null) {
                //_checkImage = Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images\\ic_checkedc.png"));
                var bmp = new BitmapImage(new Uri("pack://application:,,,/SuperKit;component/Images/ic_checkedc.png"));
                _checkImage = new Image { Source = bmp };
            }


            return _checkImage;
        }
        public static Image CheckIcon(bool check) {
            return check ? GetCheckImage() : null;
        }



        private void Application_Startup(object sender, StartupEventArgs e) {
            ArgReader.InitialArgs = new ArgReader(e.Args);
            string targetFile = null;

            // 如果指定了一个文件名，而不是指定文件，需要特别处理
            if (Kit.IsEmpty(Args.FileName) == false) {
                var info = new FileInfo(Args.FileName);
                // 如果是一个媒体文件，则使用播放器打开
                if (Kit.IsFileType(info, Kit.MEDIA_EXT)) {
                    var win = new SuperPlayer();
                    win.MediaSource = info.FullName;
                    win.Show();
                    return;
                } else {
                    targetFile = info.FullName;
                }
            }

            if (Args.IsShell) {
                var win = new ShellWindow();
                win.Args = Args.ToString();
                win.Show();
            } else {
                var startup = ConfigReader.Instance.StartupWindow;
                Window win;
                if (Kit.IsEmpty(startup)) {
                    win = new MainWindow();
                } else if (Kit.IsEqual(startup, STARTUP_RENAME)) {
                    var dlg = new BatchRename();
                    dlg.Add(new DirectoryInfo(Args.Directory));
                    win = dlg;
                } else if (Kit.IsEqual(startup, STARTUP_GALLERY)) {
                    var dlg = new GalleryWindow();           
                    dlg.DirPath = Args.Directory;
                    win = dlg;
                } else if (Kit.IsEqual(startup, STARTUP_VIDEO)) {
                    var dlg = new SuperPlayer();
                    dlg.DirPath = Args.Directory;
                    win = dlg;
                } else if (Kit.IsEqual(startup, STARTUP_HTML)) {
                    win = new HtmlWindow();
                } else {                    
                    var mainWin = new MainWindow();
                    mainWin.FileTarget = targetFile;
                    win = mainWin;
                }
                win.Show();
            }
            
            
        }
    }
}
